const http = require('http')
const url = require('url')
const responseData={
    ID:'zhangsan',
    Name:'张三',
    RegisterDate:'2023-01-01',
}
function toHTML(data){
    return `
    <ul>
        <li>ID:${data.ID}</li>
        <li>姓名:${data.Name}</li>
        <li>注册日期:${data.RegisterDate}</li>
    </ul>
    `
}
const server = http.createServer((req, res) => {
    const {pathname}=url.parse(`http://localhost:8080${req.url}`)
    if(pathname==='/'){
        const accept = req.headers.accept
        if(accept.indexOf('application/json')>=0){
            res.writeHead(200,{'Content-Type':'application/json'})
            res.end(JSON.stringify(responseData))
        }else{
            res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'})
            res.end(toHTML(responseData))
        }
    }else{
        res.writeHead(404,{'Content-Type':'text/html'})
        res.end('<h1>404 not found</h1>')
    }
})
server.listen(8080, () => {
  console.log('server is running at http://localhost:8080/')
})